<template>
  <div v-if="userAnswer.length != 0">
    <div v-for="item in userAnswer" :key="item.Id" style="position: relative">
      <div class="card" v-if="item.Status != 0">
        <span class="content">{{ item.AnswerName }}</span>
        <div class="footer">
          <span class="user">{{ item.UserName }}</span>
          <span class="time">{{ convertISO8601ToSimplified(item.Time) }}</span>
          <span class="grade">{{ item.Gradle == 0 ? '暂无评分' : item.Gradle + '分' }}</span>
          <span class="status" :class="item.Status == 0 ? 'red' : 'green'">
            {{ item.Status == 0 ? '被封禁' : '状态正常' }}
          </span>
        </div>
      </div>
      <el-button
        v-if="item.Status != 0"
        class="delete-button"
        type="warning"
        round
        plain
        @click.stop="clickDeleteAnswer(item.Id)"
        >删 除</el-button
      >
      <el-divider />
    </div>
  </div>
  <div v-else class="tishi">你还没有发表过回答哦</div>
  <el-dialog v-model="showDelete" title="删除回答" width="500" style="text-align: center">
    <div>是否确认删除回答</div>
    <div>
      <el-button type="primary" plain @click="deleteAnswer()">确 定</el-button>
      <el-button plain @click="showDelete = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { getUserAnswerAPI, deleteAnswerAPI } from '@/api/user'
import { useUserStore } from '@/stores'
// import { ElMessage } from 'element-plus'

const showDelete = ref(false)
const deleteAnswerId = ref(-1)
const clickDeleteAnswer = (AnswerId) => {
  deleteAnswerId.value = AnswerId
  showDelete.value = true
}
const deleteAnswer = async () => {
  const res = await deleteAnswerAPI({
    AnswerId: deleteAnswerId.value
  })
  if (res.data === 1) {
    await getUserAnswer()
    showDelete.value = false
    ElMessage({ message: '删除成功', type: 'success' })
  } else {
    ElMessage.error('删除失败')
  }
}

const userStore = useUserStore()
const userAnswer = ref([])

const getUserAnswer = async () => {
  const res = await getUserAnswerAPI({
    UserName: userStore.userInfo.userName
  })
  userAnswer.value = res.data
}

const convertISO8601ToSimplified = (isoString) => {
  const date = new Date(isoString)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}`
}

onMounted(() => {
  getUserAnswer()
})
</script>

<style scoped>
.delete-button {
  position: absolute;
  right: 20px;
  top: 10px;
}
.tishi {
  text-align: center;
  font-size: 40px;
  padding-top: 40px;
}

.card {
  background-color: #fff; /* 白色背景 */
  border: 1px solid #e0e0e0; /* 边框 */
  border-radius: 8px; /* 圆角 */
  padding: 15px; /* 内边距 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影 */
  margin-bottom: 20px; /* 间距 */
  transition: transform 0.2s; /* 动画 */
}

.card:hover {
  transform: scale(1.02); /* 放大效果 */
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.content {
  font-size: 18px;
  font-weight: 500;
  margin: 10px 0;
}

.user {
  font-size: 14px;
  color: #555; /* 深灰色 */
}

.time {
  font-size: 12px;
  color: #999; /* 浅灰色 */
}

.grade {
  margin-left: 15px;
  font-size: 14px;
}

.status {
  font-weight: bold;
  margin-left: 20px;
}

.red {
  color: red;
}

.green {
  color: green;
}
</style>
